/*
 * Powered By [pkuWx]
 * Web Site: http://www.wuxi.ss.pku.edu.cn
 * Since 2015 - 2015
 */


package cn.edu.pku.wxlib.teacher.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.edu.pku.wxlib.common.authority.Login;
import cn.edu.pku.wxlib.teacher.model.Teacher;
import cn.edu.pku.wxlib.teacher.service.TeacherManager;
import cn.edu.pku.wxlib.teacher.vo.query.TeacherQuery;
import cn.org.rapid_framework.web.scope.Flash;
import javacommon.base.BaseRestSpringController;
import javacommon.page.Page;

/**
 * @version 1.0
 * @since 1.0
 */

@Controller
@RequestMapping("/teacher")
public class TeacherController extends BaseRestSpringController<Teacher,java.lang.Integer>{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	private final String LIST_ACTION = "redirect:/teacher";
	
	private TeacherManager teacherManager;
	public void setTeacherManager(TeacherManager manager) {
		this.teacherManager = manager;
	}
	
	@InitBinder  
	public void initBinder(WebDataBinder binder) {  
	        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));  
	}
	   
	/** 增加了@ModelAttribute的方法可以在本controller方法调用前执行,可以存放一些共享变量,如枚举值,或是一些初始化操作 */
	@ModelAttribute
	public void init(ModelMap model) {
	}
	
	/** 列表 */
	@Login @RequestMapping
	public String index(ModelMap model,TeacherQuery query,HttpServletRequest request,HttpServletResponse response) {
		Page page = this.teacherManager.findPage(query);
		
		model.addAllAttributes(toModelMap(page, query));
		return "/teacher/index";
	}
	
	/** 显示 */
	@Login @RequestMapping(value="/{id}")
	public String show(ModelMap model,@PathVariable java.lang.Integer id) throws Exception {
		Teacher teacher = (Teacher)teacherManager.getById(id);
		model.addAttribute("teacher",teacher);
		return "/teacher/show";
	}

	/** 进入新增 */
	@Login @RequestMapping(value="/new")
	public String _new(ModelMap model,Teacher teacher,HttpServletRequest request,HttpServletResponse response) throws Exception {
		model.addAttribute("teacher",teacher);
		return "/teacher/new";
	}
	
	/** 保存新增 */
	@Login @RequestMapping(method=RequestMethod.POST)
	public String create(ModelMap model,@Valid Teacher teacher,BindingResult errors,HttpServletRequest request,HttpServletResponse response) throws Exception {
		if(errors.hasErrors()) {
			return  "/teacher/new";
		}
		
		teacherManager.save(teacher);
		Flash.current().success(CREATED_SUCCESS); 
		return LIST_ACTION;
	}
	
	/** 编辑 */
	@Login @RequestMapping(value="/{id}/edit")
	public String edit(ModelMap model,@PathVariable java.lang.Integer id) throws Exception {
		Teacher teacher = (Teacher)teacherManager.getById(id);
		model.addAttribute("teacher",teacher);
		return "/teacher/edit";
	}
	
	/** 保存更新 */
	@Login @RequestMapping(value="/{id}",method=RequestMethod.PUT)
	public String update(ModelMap model,@PathVariable java.lang.Integer id,@Valid Teacher teacher,BindingResult errors,HttpServletRequest request,HttpServletResponse response) throws Exception {
		if(errors.hasErrors()) {
			return "/teacher/edit";
		}
		
		teacherManager.update(teacher);
		Flash.current().success(UPDATE_SUCCESS);
		return LIST_ACTION;
	}
	
	/** 删除 */
	@Login @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
	public String delete(ModelMap model,@PathVariable java.lang.Integer id) {
		teacherManager.removeById(id);
		Flash.current().success(DELETE_SUCCESS);
		return LIST_ACTION;
	}

	/** 批量删除 */
	@Login @RequestMapping(method=RequestMethod.DELETE)
	public String batchDelete(ModelMap model,@RequestParam("items") java.lang.Integer[] items) {
		for(int i = 0; i < items.length; i++) {
			teacherManager.removeById(items[i]);
		}
		Flash.current().success(DELETE_SUCCESS);
		return LIST_ACTION;
	}
	
}

